import { RuleOptionalStack, RuleParse } from "../type";
import globelRuleParse from './globelRuleParse';

const switchRuleParse: RuleParse = {

	parseType: "switch",

	isNested: true,

	preHandle: (parse, rule, char) => { },

	judge: (parse, rule, char) => {
		const ruleO = rule as unknown as RuleOptionalStack;
		const ruleSt = ruleO.select;
		const parseType = parse.top && parse.top.parseType;
		if (char === "[") {
			if (parseType === "switch") {
				throw new Error("字符组不支持嵌套");
			}
			parse.push(switchRuleParse);
			ruleO.push({ ruleType: "switch" });
			return true;
		} else if (parseType === "switch" && char === "^" && ruleSt !== undefined && ruleSt.children.length === 0) {
			ruleSt.info.ruleType = "exclude";
			return true;
		} else if (char === "]") {
			if (ruleSt === undefined) {
				throw new Error("栈列表选项为空");
			}
			else if (parseType !== "switch") {
				throw new Error("无可收尾字符组");
			}
			else if (ruleSt.info.ruleData === undefined && ruleSt.children.length === 0) {
				throw new Error("字符组不能为空");
			}
			parse.pop();
			parse.push(globelRuleParse);
			return true;
		}
		return false;
	},

	// [canNested=true]
	handle: (parse, rule, char) => { return false; },
};


export default switchRuleParse;
